package com.heima.kafka.sample;

import org.apache.kafka.clients.producer.*;

import java.util.Properties;

/**
 * 消息的生产者
 */
public class ProducerQuickStart {

    public static void main(String[] args) {

        //kafka配置
        Properties properties = new Properties();
        //链接哪个kafka
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.200.130:9092");
        //消息的key和value的序列化
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        //消息确认
        properties.put(ProducerConfig.ACKS_CONFIG,"all");

        //创建一个生产者对象
        KafkaProducer<String,String> kafkaProducer = new KafkaProducer<String, String>(properties);

        //发送消息
        ProducerRecord<String,String> kvProducerRecord = new ProducerRecord<>("kafka-first-topic","keydsjfsl","hello kafka");
        //同步发送
        kafkaProducer.send(kvProducerRecord);
        //异步发送
        /*kafkaProducer.send(kvProducerRecord, new Callback() {
            @Override
            public void onCompletion(RecordMetadata recordMetadata, Exception e) {
                //假如消息发送失败
                if(e != null){
                    System.out.println("记录异常到日志中"+e);
                }
            }
        });*/

        //关闭消息通道，不然，接收不到消息
        kafkaProducer.close();
    }
}
